<?php

namespace App\Console\Commands;

use App\Models\User;
use Brick\Math\RoundingMode;
use Illuminate\Console\Command;

class PresentGoldBeans extends Command
{
	protected $signature = 'cc:present-gold-beans';

	protected $description = "Present gold beans";

	public function handle()
	{
		$data = $this->ask("Input user id and amount like: 1:10.11");

		$data = collect(explode(':', $data));

		if ($data->count() != 2) {
			$this->error('Please enter right format like: 1:10.11');
			return;
		}
		if (! $user = User::find($data->first())) {
			$this->error('Expired user does not exist.');
			return;
		}

		$amount = $data->last();

		if (! is_numeric($amount)) {
			$this->error("The amount must be a numeric.");
			return;
		}
		if ($amount <= 0) {
			$this->error("The amount must be greater than 0.");
			return;
		}

		$finalAmount = big_decimal($amount)->toScale(2, RoundingMode::HALF_UP);

		account_beans($user)->plusPresentGoldBeans($finalAmount);

		$this->info('Success.');
	}
}
